//
//  HomeBodyController.m
//  NoNameAmazing
//
//  Created by liuyang on 2017/8/21.
//  Copyright © 2017年 北京执行力科技有限公司. All rights reserved.
//

#import "HomeBodyController.h"
#import "HomeBodyTableViewCell.h"
#import "BodyInfoController.h"
#import "HomeInfoModel.h"
@interface HomeBodyController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)NSArray * homeBArray;
@property(nonatomic,strong)UITableView * bodyTableView;
@property(nonatomic,strong)HomeInfoModel * homeInfoModel;
@end

@implementation HomeBodyController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.homeBArray = [NSArray array];
    [self createTable];
    self.navigationItem.rightBarButtonItem = [UIBarButtonItem barButtonTitle:@"添加" image:nil frame:CGRectMake(0, 0, 30, 20) target:self sel:@selector(addBody)];
}


-(void)createTable
{
    _bodyTableView=[[UITableView alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, kScreen_Height-44) style:UITableViewStyleGrouped];
    _bodyTableView.delegate=self;
    _bodyTableView.dataSource=self;
    _bodyTableView.showsVerticalScrollIndicator=NO;
    _bodyTableView.showsHorizontalScrollIndicator=NO;
    _bodyTableView.userInteractionEnabled= YES ;
    
    _bodyTableView.contentInset = UIEdgeInsetsMake(0, 0, -5, 0);
    
    _bodyTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    
    [self.view addSubview:_bodyTableView];
}

-(void)requestHomePeople
{
    NSDictionary *params = @{@"userid":[us objectForKey:kUserid]};
    [NetWorkManager requestWithType:1 withUrlString:@"/selectInformation/getHomeMember.do" withParaments:params withSuccessBlock:^(id object) {
        if ([object[@"message"]isEqualToString:@"成功"]) {
            self.homeBArray = [HomeInfoModel mj_objectArrayWithKeyValuesArray: object[@"homeMemberList"]];
            [self.bodyTableView reloadData];
        }
        
    } withFailureBlock:^(NSError *error) {
        
        
    } progress:^(float progress) {
        
    }];
}

#pragma mark - 数据源
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return self.homeBArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    _homeInfoModel = self.homeBArray[indexPath.row];
        HomeBodyTableViewCell *cell = [[[NSBundle mainBundle] loadNibNamed:@"HomeBodyTableViewCell" owner:nil options:nil] lastObject];
        cell.selectionStyle = UITableViewCellSelectionStyleDefault;
       cell.titleLab.text =_homeInfoModel.GXMC;
       cell.nameLab.text =_homeInfoModel.CYXM;
        return cell;
}
#pragma mark - 代理方法
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
       return 43;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0.0001;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BodyInfoController *bodyInfoController = [[BodyInfoController alloc] init];
    bodyInfoController.homeInfoModel = self.homeBArray[indexPath.row];
    bodyInfoController.typy = @"update";
    [self.navigationController pushViewController:bodyInfoController animated:YES];
}


-(void)addBody
{
    BodyInfoController *bodyInfoController = [[BodyInfoController alloc] init];
    bodyInfoController.homeInfoModel = [[HomeInfoModel alloc]init];
     bodyInfoController.typy = @"add";
    [self.navigationController pushViewController:bodyInfoController animated:YES];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:YES];
     [self requestHomePeople];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
